package com.topcoder.srm514;

import java.util.LinkedList;

public class MagicalGirlLevelTwoDivTwo {
	public String isReachable(int[] jumpTypes, int x, int y) {
		boolean[][] visited = new boolean[61][61];
		LinkedList<Point> queue = new LinkedList<Point>();
		queue.add(new Point(0, 0));
		
		while (!queue.isEmpty()) {
			Point p = queue.remove();
			if (p.x == x && p.y == y) return "YES";
			
			if (p.x < -30 || p.x > 30 || p.y < -30 || p.y > 30 || 
					visited[p.x+30][p.y+30]) continue;
			visited[p.x+30][p.y+30] = true;
			
			for (int n : jumpTypes) {
				queue.add(new Point(p.x + n, p.y + 1));
				queue.add(new Point(p.x + n, p.y - 1));
				queue.add(new Point(p.x - n, p.y + 1));
				queue.add(new Point(p.x - n, p.y - 1));
				queue.add(new Point(p.x + 1, p.y + n));
				queue.add(new Point(p.x + 1, p.y - n));
				queue.add(new Point(p.x - 1, p.y + n));
				queue.add(new Point(p.x - 1, p.y - n));
			}
		}
		
		return "NO";
	}
	
	private class Point {
		int x, y;
		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}
	
	// Online solution: Mathematical way.
	public String isReachable2(int[] jumpTypes, int x, int y) {
		if ((x+y) % 2 == 0) return "YES";
		for (int n : jumpTypes) {
			if (n % 2 == 0) return "YES";
		}
		return "NO";
	}
}
